﻿using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using YunNanHealthProject_Register.service;
using YunNanHealthProject_Register.Utils;
using YunNanHealthProject_Register.View;

namespace YunNanHealthProject_Register
{
    public partial class RegisterForm : MetroForm
    {
        private UpLoadInfoService upLoadInfoService = new UpLoadInfoService();
        private const string INIPATH = ".\\config\\TextFile.ini";
        private static string LogPath = "";
        AutoSizeFormClass asc = new AutoSizeFormClass();
        System.Timers.Timer timer = new System.Timers.Timer();
        Dictionary<string, string> fileDic = new Dictionary<string, string>();
        Dictionary<string, string> logDic = new Dictionary<string, string>();

        public RegisterForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            LogPath = IniHelper.ReadIni("LogPath", "path", INIPATH);
            PathLabel.ForeColor = Color.Black;
            PathLabel.Text = LogPath;
            fileDic = InitListBox(LogPath, 1, FileListBox);
            logDic = InitListBox(fileDic["info"], 2, LogListBox);
            FileListBox.SelectedIndex = 1;
            LogListBox.SelectedIndex = 0;
        }

        //页面自适应
        private void RegisterForm_Load(object sender, EventArgs e)
        {
            asc.controllInitializeSize(this);
        }

        //页面改变时自适应
        private void RegisterForm_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        //基本信息注册
        private void HospitalInfoButton_Click(object sender, EventArgs e)
        {
            HospitalInfoForm form = new HospitalInfoForm();
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }

        //一级科室信息
        private void Level1KSButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(upLoadInfoService.UpLevel1KS());
        }

        //二级科室信息
        private void Level2Button_Click(object sender, EventArgs e)
        {
            MessageBox.Show(upLoadInfoService.UpLevel2KS());
        }

        //上传医生基本信息
        private void UpYSInfoButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(upLoadInfoService.UpYSInfo());
        }

        //门诊信息注册
        private void MZInfoButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(upLoadInfoService.UpMZInfo());
        }

        //停诊上传
        private void StopWorkButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(upLoadInfoService.UpStopScheduleInfo());
            NumericUpDown.Enabled = true;
        }

        //号源变动通知
        private void ChangeSourceNum_Click(object sender, EventArgs e)
        {
        }

        //预约就诊情况上传
        private void UpOrderMedicin_Click(object sender, EventArgs e)
        {
            MessageBox.Show(upLoadInfoService.UpOrderMedicin());
        }

        //定时执行停诊医师上传
        private void StopMetroButton_Click(object sender, EventArgs e)
        {
            NumericUpDown.Enabled = false;
            timer.Enabled = true;
            timer.Interval = 1000 * 60 * int.Parse(NumericUpDown.Value.ToString());
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer1_Elapsed);
            ResultMetroLabel.Text = "医师停诊上传以开始定时上传";
        }

        //停止定时上传停诊医师
        private void StopButton_Click(object sender, EventArgs e)
        {
            timer.Enabled = false;
            ResultMetroLabel.Text = "医师停诊上传已停止执行";
        }

        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            ResultMetroLabel.Text = "上一次执行时间" + DateTime.Now + "结果" + upLoadInfoService.UpStopScheduleInfo();
        }

        private void OpenLogFileButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择Txt所在文件夹";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
                IniHelper.WriteIni("LogPath", "path", dialog.SelectedPath, INIPATH);
                PathLabel.ForeColor = Color.Black;
                PathLabel.Text = dialog.SelectedPath;
                InitListBox(InitListBox(dialog.SelectedPath, 1, FileListBox)["info"], 2, LogListBox);
            }
        }

        //

        //1.获取文件夹,2.获取文件
        private Dictionary<string, string> InitListBox(string path, int flag, ListBox listBox)
        {
            listBox.Items.Clear();
            DirectoryInfo root = new DirectoryInfo(path);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (flag == 1)
            {

                foreach (DirectoryInfo dics in root.GetDirectories())
                {
                    listBox.Items.Add(dics.Name);
                    dic.Add(dics.Name, dics.FullName);
                }
                return dic;
            }
            else if (flag == 2)
            {
                FileInfo[] file = root.GetFiles();
                for (int i = file.Length - 1; i > 0; i--)
                {
                    listBox.Items.Add(file[i].Name);
                    dic.Add(file[i].Name, file[i].FullName);
                }
                return dic;
            }
            return null;
        }

        //日记选取日志类型
        private void FileListBox_Click(object sender, EventArgs e)
        {
            if (FileListBox.SelectedIndex != -1)
            {
                InitListBox(fileDic[FileListBox.SelectedItem.ToString()], 2, LogListBox);
            }
        }

        //读取日志文件
        private void LogListBox_Click(object sender, EventArgs e)
        {
            if (LogListBox.SelectedIndex != -1)
            {
                FileStream fs = new FileStream(logDic[LogListBox.SelectedItem.ToString()], FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.Default);
                string strLine = sr.ReadLine();
                string str = "";
                while (strLine != null)
                {
                    str += strLine.ToString() + "|";
                    strLine = sr.ReadLine();
                }
                sr.Close();
                fs.Close();
                string[] logArr = str.Split('|');
                MessageBox.Show(logArr.Length.ToString());
                //string titel = str.Substring(0, str.IndexOf('|'));
                //MessageBox.Show(str.Substring(0, str.IndexOf('|')));
                //MessageBox.Show(str.Substring(str.IndexOf('【'), str.IndexOf(':')));
                //string titel;
                //int startPostion, endPosition;
                //for (int i = 0; i < str.Length; i++)
                //{
                //}
            }
        }



    }
}